import { request } from '@/api/request';
import { useDeviceStore } from '@/store/deviceStore';

export function postAction<T = void>(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request<T>({
    url,
    method: 'POST',
    data,
    ...(config || {})
  });
}

export function putAction<T = void>(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request<T>({
    url,
    method: 'PUT',
    data,
    ...(config || {})
  });
}

export function deleteAction<T = void>(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request<T>({
    url,
    method: 'DELETE',
    data,
    ...(config || {})
  });
}

export function getAction<T = void>(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request<T>({
    url,
    method: 'GET',
    data,
    ...(config || {})
  });
}

export function postActionFormData<T = void>(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request<T>({
    url,
    method: 'POST',
    data,
    header: {
      'Content-Type': 'multipart/form-data'
    },
    ...(config || {})
  });
}

export function fileDown(url: string, data: Props = {}, config?: Partial<RequestOptions>) {
  return request({
    url,
    data,
    responseType: 'arraybuffer',
    ...(config || {})
  });
}

export function buildRequestURL(path: string) {
  const { buildRequestBaseURL } = useDeviceStore();
  const url = path.startsWith('/')
    ? path.slice(1)
    : path;
  return buildRequestBaseURL() + url;
}
